﻿using Kenly.DBFramework.Reflection;
using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Reflection;

using Kenly.DBFramework.TestModel.HumanResources;
using Kenly.DBFramework.TestModel.Person;

namespace Kenly.DBFramework.Tests
{


    /// <summary>
    ///这是 KeyUtilityTest 的测试类，旨在
    ///包含所有 KeyUtilityTest 单元测试
    ///</summary>
    [TestClass()]
    public class KeyUtilityTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///SetPrimaryKey 的测试
        ///</summary>
        [TestMethod()]
        public void SetPrimaryKeyTest()
        {
            Employee entity = new Employee { EmployeeID = 1 };

            KeyUtility.SetPrimaryKey(entity, 2);
            Assert.IsTrue(entity.EmployeeID == 2);

            IViewGateway view = new Employee { EmployeeID = 1 };

            KeyUtility.SetPrimaryKey(view, 2);
            Assert.IsTrue(view.PrimaryKey.ToString() == "2");

            KeyUtility.SetPrimaryKey(null, 2);
            KeyUtility.SetPrimaryKey(new object(), 2);
        }

        /// <summary>
        ///GetPrimaryKeyName 的测试
        ///</summary>
        [TestMethod()]
        public void GetPrimaryKeyNameTest()
        {
            string actual = KeyUtility.GetPrimaryKeyName(null);
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = KeyUtility.GetPrimaryKeyName(typeof(object));
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = KeyUtility.GetPrimaryKeyName(typeof(Employee));
            Assert.IsTrue(actual == "EmployeeID");
        }

        /// <summary>
        ///GetPrimaryKey 的测试
        ///</summary>
        [TestMethod()]
        public void GetPrimaryKeyTest()
        {
            Employee entity = new Employee { EmployeeID = 1 };
            string key = string.Empty;
            object value = null;

            KeyUtility.GetPrimaryKey(null, out key, out value);
            Assert.IsTrue(string.IsNullOrEmpty(key));
            Assert.IsNull(value);

            KeyUtility.GetPrimaryKey(new object(), out key, out value);
            Assert.IsTrue(string.IsNullOrEmpty(key));
            Assert.IsNull(value);

            KeyUtility.GetPrimaryKey(entity, out key, out value);
            Assert.IsTrue(key == "EmployeeID");
            Assert.IsTrue(value.ToString() == "1");
        }

        /// <summary>
        ///GetKeyValue 的测试
        ///</summary>
        [TestMethod()]
        public void GetKeyValueTest()
        {
            Employee entity = new Employee { EmployeeID = 1, ContactID = 100 };
            string key = "EmployeeID";

            object actual = KeyUtility.GetKeyValue(entity, null);
            Assert.IsNull(actual);

            actual = KeyUtility.GetKeyValue(entity, string.Empty);
            Assert.IsNull(actual);

            actual = KeyUtility.GetKeyValue(null, key);
            Assert.IsNull(actual);

            actual = KeyUtility.GetKeyValue(null, null);
            Assert.IsNull(actual);

            actual = KeyUtility.GetKeyValue(new object(), key);
            Assert.IsNull(actual);

            actual = KeyUtility.GetKeyValue(entity, "invalidkey");
            Assert.IsNull(actual);

            actual = KeyUtility.GetKeyValue(entity, key);
            Assert.IsTrue(actual.ToString() == "1");

            actual = KeyUtility.GetKeyValue(entity, "ContactID");
            Assert.IsTrue(actual.ToString() == "100");
        }

        /// <summary>
        ///GetForeignKey 的测试
        ///</summary>
        [TestMethod()]
        public void GetForeignKeyTest()
        {
            Type type = typeof(Employee);
            Type referenceType = typeof(Contact);

            string actual = KeyUtility.GetForeignKey(type, null);
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = KeyUtility.GetForeignKey(null, referenceType);
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = KeyUtility.GetForeignKey(null, null);
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = KeyUtility.GetForeignKey(typeof(object), referenceType);
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = KeyUtility.GetForeignKey(type, typeof(object));
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = KeyUtility.GetForeignKey(type, referenceType);
            Assert.IsTrue(actual == "ContactID");
        }

        /// <summary>
        ///GetPrimaryKeyProperty 的测试
        ///</summary>
        [TestMethod()]
        public void GetPrimaryKeyPropertyTest()
        {
            PropertyInfo actual = KeyUtility.GetPrimaryKeyProperty(null);
            Assert.IsNull(actual);

            actual = KeyUtility.GetPrimaryKeyProperty(typeof(object));
            Assert.IsNull(actual);

            actual = KeyUtility.GetPrimaryKeyProperty(typeof(Employee));
            Assert.IsTrue(actual.Name == "EmployeeID");
        }
    }
}
